/**
* \file	SymbolTable.cpp
* \author Caio Arce Nishibe (caionishibe@gmail.com)
* \date	August, 2014
* \brief Symbol Table Implementation.
*/
#include "SymbolTable.h"


SymbolTable::SymbolTable()
{
	this->table = NULL;
	this->table = new Symbol();
}

SymbolTable::~SymbolTable()
{
	Symbol *first;
	Symbol *actual;

	if (table != NULL)
	{


		first = table->next;
		table->next = NULL;

		while (first != NULL)
		{
			actual = first;
			first = first->next;

			delete actual;
		}
	}
}

Symbol* SymbolTable::LookupSymbol(string name, string owner)
{
	Symbol *symbol;

	if (this->table == NULL)
		return NULL;

	for (symbol = table->next; symbol != NULL; symbol = symbol->next)
	{
		if (symbol->name == name && symbol->owner == owner)
			return symbol;
	}

	return symbol;
}

Symbol* SymbolTable::InsertSymbol(Symbol *symbol)
{
	Symbol *sym;

	if (symbol == NULL)
		return NULL;

	//before insert the symbol to the table, we
	//have to check if the symbol already exists
	sym = LookupSymbol(symbol->name, symbol->owner);

	if (sym != NULL)
	{
		cerr << "Error: A symbol with the name " << "\"" << sym->name << "\" and same owner already exists!" << endl;
		delete symbol;
		exit(1);
	}

	symbol->next = table->next;
	table->next = symbol;

	return symbol;
}




